package com.echo.week.web;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;

/**
 * @Author echo
 * @Description
 * @Date 2023/10/30.
 * @Version 1.0
 */
public class QueueDemo {

    private static final Queue<String> msgQuery = new ConcurrentLinkedQueue<>();

    public static void main(String[] args) {

        new Thread(new AddMsg()).start();
        new Thread(new GetMsg()).start();

        System.out.println("----------------------------------------");
        List<String> msgS = new ArrayList<>();
        for (; ; ) {
            String poll = msgQuery.poll();
            if (poll != null) {
                msgS.add(poll);
            } else {
                break;
            }
        }
        System.out.println(msgS);

    }

    static class AddMsg implements Runnable {

        @Override
        public void run() {
            for (int i = 0; i < 10; i++) {
                msgQuery.offer(String.valueOf(i));
            }
        }
    }

    static class GetMsg implements Runnable {

        @Override
        public void run() {
            List<String> msgS = new ArrayList<>();
            for (; ; ) {
                String poll = msgQuery.poll();
                if (poll != null) {
                    msgS.add(poll);
                } else {
                    break;
                }
            }
            System.out.println("old"+msgS);
        }
    }
}
